package com.hmkj.core.api;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.api.alibaba.AliyunSmsUtil;
import com.hmkj.core.api.chuanglan.util.ChuangLanSmsUtil;
import com.hmkj.core.api.zhutong.util.ZhuTongSmsUtil;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.service.cache.RedisService;

import java.util.Map;

/**
 * api接口工具类 Created by Donfy on 2017/6/19.
 */
public class APIUtils {

    private static class API {

        /**
         * 创蓝短信接口工具类
         */
        private static final ChuangLanSmsUtil CHUANG_LAN_SMS_UTIL = new ChuangLanSmsUtil(z -> {
            RedisService redisService = SpringContextUtils.getBean(RedisService.class);
            z.setSignature(redisService.get(ConfigNID.SMS_SIGN));
            z.setUsername(redisService.get(ConfigNID.SMS_CL_USERNAME));
            z.setPassword(redisService.get(ConfigNID.SMS_CL_PWD));
            z.setSmsUrl(redisService.get(ConfigNID.SMS_CL_URL));
            z.setEnable(redisService.get(ConfigNID.SMS_OPEN, Boolean.class));
        });

        /**
         * 助通短信接口工具类
         */
        private static final ZhuTongSmsUtil ZHU_TONG_SMS_UTIL = new ZhuTongSmsUtil(z -> {
            RedisService redisService = SpringContextUtils.getBean(RedisService.class);
            z.setSignature(redisService.get(ConfigNID.SMS_SIGN));
            z.setUsername(redisService.get(ConfigNID.SMS_ZT_USERNAME));
            z.setPassword(redisService.get(ConfigNID.SMS_ZT_PWD));
            z.setSmsUrl(redisService.get(ConfigNID.SMS_ZT_URL));
            z.setEnable(redisService.get(ConfigNID.SMS_OPEN, Boolean.class));
        });

        /**
         * 阿里云短信接口工具类
         */
        private static final AliyunSmsUtil ALIYUN_SMS_UTIL = new AliyunSmsUtil(z -> {
            RedisService redisService = SpringContextUtils.getBean(RedisService.class);
            z.setAccessKeyId(redisService.get(ConfigNID.OSS_ACCESSKEY_ID));
            z.setAccessKeySecret(redisService.get(ConfigNID.OSS_ACCESSKEY_SECRET));
            z.setEnable(redisService.get(ConfigNID.SMS_OPEN, Boolean.class));
        });

    }



    /**
     * 发送短信(创蓝)
     *
     * @param mobile  手机号码
     * @param content 内容
     * @return 结果
     * @throws Exception
     */
    public static String sendSmsCL(String mobile, String content) throws Exception {
        return API.CHUANG_LAN_SMS_UTIL.sendSmsByPost(mobile, content);
    }

    /**
     * 发送短信（助通）
     *
     * @param mobile  手机号码
     * @param content 内容
     * @return 结果
     * @throws Exception
     */
    public static String sendSmsZT(String mobile, String content) throws Exception {
        return API.ZHU_TONG_SMS_UTIL.sendSmsByPost(mobile, content);
    }

    /**
     * 发送短信（阿里云）
     *
     * @param param 模板参数
     * @return 结果
     * @throws Exception
     */
    public static SendSmsResponse sendSmsAL(Map<String,Object> param) throws Exception {
        return API.ALIYUN_SMS_UTIL.sendSmsByPost(param);
    }

}
